import java.util.Scanner;

public class Question5 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int times = scanner.nextInt();
        int[][] input = new int[2][times];
        int levels = -1;
        int res = 0;
        for (int i = 0; i < times; i ++){
            input[0][i] = scanner.nextInt();
            input[1][i] = scanner.nextInt();
        }
        for (int i = 0;i < times;i++){
            for (int n = 0; n < 18 ;n ++){
                if (Math.pow(2,n) <= input[0][i] && Math.pow(2,n + 1) > input[0][i]){
                    levels = n + 1;
                    break;
                }
            }
            if (levels != -1 && levels > input[1][i]){
                res = input[0][i];
                for (int j = 1;j <= levels - input[1][i]; j ++){
                    res = res / 2;
                }
                System.out.println(res);
            }else {
                System.out.println(-1);
            }
            levels = -1;
        }
    }
}
